package com.qingyouchuxing.mall.biz.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

import com.qingyouchuxing.mall.biz.domain.bo.MallGoodsClassifyBO;
import com.qingyouchuxing.mall.biz.domain.dto.MallGoodsClassifyFromDTO;
import com.qingyouchuxing.mall.biz.domain.entity.MallGoodsClassify;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qingyouchuxing.mall.biz.mapper.MallGoodsClassifyMapper;
import com.qingyouchuxing.mall.biz.domain.vo.MallGoodsClassifyVO;
import com.qingyouchuxing.mall.biz.service.IMallGoodsClassifyService;
import static com.qingyouchuxing.mall.biz.domain.entity.table.MallGoodsClassifyTableDef.MALL_GOODS_CLASSIFY;

/**
 * 商品产品分类Service业务层处理
 *
 * @author 数据小王子
 * 2024-09-06
 */
@Service
class MallGoodsClassifyServiceImpl extends BaseServiceImpl<MallGoodsClassifyMapper, MallGoodsClassify> implements IMallGoodsClassifyService
{
    @Resource
    private MallGoodsClassifyMapper mallGoodsClassifyMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(MALL_GOODS_CLASSIFY);
    }

    private QueryWrapper buildQueryWrapper(MallGoodsClassifyBO mallGoodsClassifyBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(MALL_GOODS_CLASSIFY.NAME.like(mallGoodsClassifyBo.getName()));
        queryWrapper.and(MALL_GOODS_CLASSIFY.FK_TENANT_ID.eq(mallGoodsClassifyBo.getFkTenantId()));

        return queryWrapper;
    }

    /**
     * 查询商品产品分类
     *
     * @param id 商品产品分类主键
     * @return 商品产品分类
     */
    @Override
    public MallGoodsClassifyVO selectById(Long id)
    {
        return this.getOneAs(query().where(MALL_GOODS_CLASSIFY.ID.eq(id)), MallGoodsClassifyVO.class);

    }

    /**
     * 查询商品产品分类列表
     *
     * @param mallGoodsClassifyBO 商品产品分类BO
     * @return 商品产品分类集合
     */
    @Override
    public List<MallGoodsClassifyVO> selectList(MallGoodsClassifyBO mallGoodsClassifyBO)
    {
        QueryWrapper queryWrapper = buildQueryWrapper(mallGoodsClassifyBO);
        return this.listAs(queryWrapper, MallGoodsClassifyVO.class);
    }

    /**
     * 分页查询商品产品分类列表
     *
     * @param mallGoodsClassifyBO 商品产品分类Bo
     * @return 分页商品产品分类集合
     */
    @Override
    public TableDataInfo<MallGoodsClassifyVO> selectPage(MallGoodsClassifyBO mallGoodsClassifyBO)
    {
        QueryWrapper queryWrapper = buildQueryWrapper(mallGoodsClassifyBO);
        Page<MallGoodsClassifyVO> page = this.pageAs(PageQuery.build(), queryWrapper, MallGoodsClassifyVO.class);
        return TableDataInfo.build(page);
    }

    /**
     * 查询商品产品分类列表
     *
     * @param mallGoodsClassifyBO 商品产品分类Bo
     * @return 商品产品分类集合
     */
    @Override
    public List<MallGoodsClassifyVO> selectAllGoodsClassify(MallGoodsClassifyBO mallGoodsClassifyBO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<MallGoodsClassify> mallGoodsClassifies = mallGoodsClassifyMapper.selectListByQuery(queryWrapper);
        // 将查询到的商品分类信息转换为 MallGoodsClassifyVO 对象列表
        List<MallGoodsClassifyVO> MallGoodsClassifyVOs = mallGoodsClassifies.stream()
            .map(this::convertToVo)
            .collect(Collectors.toList());
        return MallGoodsClassifyVOs;
    }

    // 将 MallGoodsClassify 对象转换为 MallGoodsClassifyVO 对象的方法
    private MallGoodsClassifyVO convertToVo(MallGoodsClassify mallGoodsClassify) {
        MallGoodsClassifyVO MallGoodsClassifyVO = new MallGoodsClassifyVO();
        // 在这里进行对象属性的复制或转换
        BeanUtils.copyProperties(mallGoodsClassify, MallGoodsClassifyVO);
        return MallGoodsClassifyVO;

    }

    /**
     * 新增商品产品分类
     * @param dto 商品产品分类DTO
     * @return
     */
    @Override
    public boolean insert(MallGoodsClassifyFromDTO dto)
    {
        MallGoodsClassify mallGoodsClassify = MapstructUtils.convert(dto, MallGoodsClassify.class);

        return this.save(mallGoodsClassify);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 修改商品产品分类
     * @param dto 商品产品分类DTO
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(MallGoodsClassifyFromDTO dto)
    {
        MallGoodsClassify mallGoodsClassify = MapstructUtils.convert(dto, MallGoodsClassify.class);
        if(ObjectUtil.isNotNull(mallGoodsClassify) && ObjectUtil.isNotNull(mallGoodsClassify.getId())) {
            int currentVersion = getCurrentVersionFromDB(mallGoodsClassify.getId());
            // 设置版本号到实体对象
            mallGoodsClassify.setVersion(currentVersion);
            boolean updated = this.updateById(mallGoodsClassify);
            return updated;
        }
        return false;
    }

    /**
     * 根据id获取版本号
     * @param id
     * @return
     */
    private int getCurrentVersionFromDB(Long id) {
        MallGoodsClassify mallGoodsClassify = mallGoodsClassifyMapper.selectOneById(id);
        Integer version = mallGoodsClassify.getVersion();
        return version;
    }

    /**
     * 批量删除商品产品分类
     *
     * @param ids 需要删除的商品产品分类主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids)
    {
        return this.removeByIds(Arrays.asList(ids));
    }

}
